const request = require('superagent');

const CONFIG = require('../../../../../sjgsvn/server/common/config');
const API_SERVER_URL = CONFIG.API_SERVER_FULL_URL;

const parserResponse = (response) => {

    const res = JSON.parse(response.text);
    if(res.success === 1) {
        return res.data;
    }
    throw new Error(res.msg, res.success);
};


module.exports = (url, options) => {
    url = API_SERVER_URL + url;
    options = options || {method : 'GET'};

    const method = options.method || 'GET';
    const body = options.body;


    if(method.toUpperCase() === 'GET') {
        return request
            .get(url)
            .send(body)
            .then(parserResponse)
    } else {
        return request
            .post(url)
            .send(body)
            .then(parserResponse);
    }
};